"use strict";
var models = require('./../models');
var co = require('co');
var q = require('q');
var LineFit = require('./LineFit');
var DistHelper = require('./DistHelper.js');
var AngleHelper = require('./AngleHelper.js');
var TpsHelper = require('./TpsHelper.js');
var SocketHelper = require('./SocketHelper.js');
var SurveyFitLine = require('./SurveyFitLine.js');

/** 
 * 用来控制顶管测量的控制器
 */
class PipeController {
    /** 
     * 创建的选项
     * @param {object} option 配置选项
     */
    constructor(option) {
        this.option = option;
        this.distHelper = null;//留空
        this.tpsHelper = null;//留空
        this.socketHelper = new SocketHelper();
        this.stationConfig = null;//测站信息
        this.prismConfigList = [];//棱镜配置列表
        this.angleHelper = null;
        this.surveyFitLine = null;
    }
    
    /** 
     * 初始化测距模块
     * @param {object} device DistDeviceConfig 测距仪配置 
     */
    initialDist(com, device) {
        var _this = this;
        this.distCom = com;
        _this.distDevice = device;
        return co(function* () {
            if (_this.distHelper) {
                yield _this.distHelper.dispose();
                yield q.delay(800);
            }
            _this.distHelper = new DistHelper({
                com: com,
                device: device
            });
            yield q.delay(800);
        });
    }

    /** 
     * @todo
     * 初始化全站仪模块
     */
    initialTps(com, stationConfig) {
        this.tpsCom = com;
        this.stationConfig = stationConfig;
        var _this = this;
        return co(function* () {
            if (_this.tpsHelper) {
                yield _this.tpsHelper.dispose();
                yield q.delay(800);
            }
            var line = yield SurveyFitLine.getLineFromDb(_this.stationConfig.prjId);
            _this.tpsHelper = new TpsHelper({
                com: com,
                stationConfig: _this.stationConfig,
                surveyFitLine: line
            });
            yield q.delay(800);
        });
    }

    /** 
     * 初始化倾角计测量帮助类
     */
    initialAngle(com, devices) {
        this.angleCom = com;
        this.angleDevices = devices;
        var _this = this;
        return co(function* () {
            if (_this.angleHelper) {
                yield _this.angleHelper.dispose();
                yield q.delay(800);
            }
            _this.angleHelper = new AngleHelper({
                com: com,
                devices: devices
            });
            yield q.delay(800);
        });
    }

    /** 
     * 开始测量
     */
    startSurvey() {
        var _this = this;
        return co(function* () {
            yield _this.initialDist(_this.distCom, _this.distDevice);
            yield _this.initialTps(_this.tpsCom, _this.stationConfig);
            // yield _this.initialAngle(_this.angleCom,_this.angleDevices);
            _this.tpsHelper.distDevice = _this.distHelper.device;//把测距仪发送给全站仪控制器
            // _this.tpsHelper.angleDeviceList = _this.angleHelper.deviceList;//把倾角计的设备列表告诉全站仪
            yield q.delay(2000);
            _this.distHelper.startSurvey();
            _this.tpsHelper.startSurvey();
            // _this.angleHelper.startSurvey();
        });
    }


    
    /** 
     * 停止测量
     */
    stopSurvey() {
        this.distHelper && this.distHelper.removeAllListeners();
        this.tpsHelper && this.tpsHelper.removeAllListeners();
        this.angleHelper && this.angleHelper.removeAllListeners();
        this.distHelper.stopSurvey();
        this.tpsHelper.stopSurvey();
        this.angleHelper && this.angleHelper.stopSurvey();
    }

}

module.exports = PipeController;